//
//  WBTabBarController.swift
//  Weibo
//
//  Created by xu on 2017/3/18.
//  Copyright © 2017年 xuantong. All rights reserved.
//

import UIKit


class WBTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        //MARK: 子控制器
        addChildViewControllers()
        //MARK: 自定义tabBar
        //实例化
        let WBtabBar = WBTabBar()
        //只读属性不能直接赋值,使用kvc进行赋值
        
        self.setValue(WBtabBar, forKey: "tabBar")
        
        //监听点击事件
        //实际开发中,按钮的点击事件处理的地方 需要留一个查找的标记 方便后面来做按钮点击的事件
        
        WBtabBar.composrClosure = {[weak self] in
            
//            let compose = WBComposeView()
//            self?.view.addSubview(compose)
            WBComposeView.show(target: self!)
        }
        
    }
   
    
    //添加子视图控制器 -- private--私有的
   private func addChildViewControllers () {
    
    addChildViewController(vc: WBHomeTableViewController(), title: "首页", imageName: "tabbar_home")
    
    addChildViewController(vc: WBMessageTableViewController(), title: "消息", imageName: "tabbar_message_center")

    addChildViewController(vc: WBDiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")

    addChildViewController(vc: WBProfileTableViewController(), title: "我", imageName: "tabbar_profile")

    
    }
    
    
    //MARK: 添加子视图控制器
    private func addChildViewController(vc:UIViewController,title:String,imageName:String) {
        
        //1.需要同时设置title和图片才能够现实
        vc.tabBarItem.image = UIImage(named: imageName)
        
        //选中的图片
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        
        vc.tabBarItem.title = title
        
        vc.navigationItem.title = title
        
        //设置文字颜色
      vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor .orange], for: .selected)
        
        //设置文字的大小
      vc.tabBarItem.setTitleTextAttributes([NSFontAttributeName :UIFont.systemFont(ofSize: 10)], for: .normal)
        
        //设置文字和图片的间距
        vc.tabBarItem.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: -3)
        
        //2.将tabvc包装到导航控制器
        let nav = WBController(rootViewController: vc)
        
        //3将导航控制器添加到TabBarcontroller
        self.addChildViewController(nav)
        
    }
    

}
